package top.suven.base.http.admin.sec.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.sec.dao.RoleDao;
import top.suven.base.http.admin.sec.model.AdminRole;
import top.suven.base.http.admin.sec.model.Role;
import top.suven.base.http.admin.sec.service.RoleService;
import top.suven.base.http.admin.sec.vo.response.RoleResponseVo;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.util.utilize.BeanUtil;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title: RoleService.java
 * @Description: 角色的业务处理服务类
 * @author lixiangling
 * @date   2018/08/13 16:58:08
 * @version V1.0.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 *
 */
@Component("roleService")
public class RoleServiceImpl implements RoleService {
    private final static Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);
    @Autowired
    private RoleDao roleDao;
    /**
     * @Title: 获取角色分页信息
     * @Description:
     * @param role @{Link Role}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:08
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public PageData<RoleResponseVo> getRolePage(Role role, int start, int pageSize){
        int count = roleDao.countEntity(role);
        if(count == 0){
            return new PageData<RoleResponseVo>(Collections.EMPTY_LIST,count);
        }
        List<Role> roleList = roleDao.findPageEntity(role, start, pageSize);
        if(roleList == null || roleList.isEmpty()){
            return new PageData<RoleResponseVo>(Collections.EMPTY_LIST,count);
        }
        List<RoleResponseVo> roleResponseList = roleList.stream().map(roleResponse -> {
            RoleResponseVo roleResponseVo = new RoleResponseVo();
            BeanUtil.copyProperties(roleResponse, roleResponseVo);
            return roleResponseVo;
        }).collect(Collectors.toList());
        return new PageData<RoleResponseVo>(roleResponseList,count);
    }
    /**
     * @Title: 获取角色列表信息
     * @Description:
     * @param role @{Link Role}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:08
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public List<Role> getRoleList(Role role){
        return roleDao.findEntity(role);
    }

    @Override
    public List<Role> getRoleValidListByAdminCode(AdminRole adminRole) {
        return roleDao.getRoleValidListByAdminCode(adminRole);
    }

    /**
     * @Title: 新增角色信息
     * @Description:
     * @param role @{Link Role}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:08
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public void addRole(Role role){
        roleDao.createEntity(role);
    }
    /**
    * @Title: 修改角色信息
    * @Description:
    * @param role @{Link Role}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:08
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public void updRole(Role role){
        roleDao.updateEntity(role);
    }
   /**
    * @Title: 获取角色信息
    * @Description:
    * @param id @{Link String}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:08
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public Role getRole(String id){
        Role role = new Role();
        role.setRoleCode(id);
        return roleDao.retrieveEntity(role);
    }
    /**
     * @Title: 删除角色信息
     * @Description:
     * @param ids @{Link String[]}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:08
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    public void del(String[] ids){
        roleDao.del(Arrays.asList(ids));
    }
   /**
    * @Title: 启用角色信息
    * @Description:
    * @param ids @{Link String[]}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:08
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public void turnOn(String[] ids){
        roleDao.updateEnableStatus(EnableStatusEnum.ENABLE.getIndexStr(), Arrays.asList(ids));
    }
   /**
    * @Title: 禁用角色信息
    * @Description:
    * @param ids @{Link String[]}
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:08
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    public void turnOff(String[] ids){
        roleDao.updateEnableStatus(EnableStatusEnum.DISABLE.getIndexStr(), Arrays.asList(ids));
    }
}